package shopping.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/test")
public class TestServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        System.out.println("前端传来的:" + username);

        response.setHeader("Access-Control-Allow-Origin","http://api.xyshopping.com:8848");
        //尝试获取Cookie
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println("cookie的名字："+cookie.getName()+"Cookie值："+cookie.getValue());
        }
        Cookie cookie = new Cookie("xyz", "woshiCookie");
        cookie.setMaxAge(60*60);
        cookie.setPath("/");
        //响应给客户端
        response.addCookie(cookie);

        response.getWriter().print("测试Cookie");
    }
}
